package com.xzy.java.event;

import java.util.LinkedList;
import java.util.List;

/**
 * @author xzy.xiao
 * @date 2023/3/14  10:11
 */
public class Source {

    public String data = "";

    /**
     * 监听器
     */
    public List<MyEventListener> listenerList = new LinkedList<>();

    public void registerListener(MyEventListener myEventListener) {
        this.listenerList.add(myEventListener);
    }

    public void dataChange(String newValue) {
        String msg = "data change：" + data + "→" + newValue;
        this.data = newValue;

        // 发布事件
        for (MyEventListener myEventListener : listenerList) {
            myEventListener.handleMsg(new MyEventObject(Source.class, msg));
        }
    }


}
